<?php

namespace app\factory;

use app\model\agency\AgencyOrderModel;
use app\model\order\OrderModel;
use app\model\order\PayLogModel;
use app\model\store\StoreDepositModel;
use app\model\user\UserMemberOrderModel;
use app\model\user\UserRechargeOrderModel;
use think\facade\Db;

/**
 * 支付处理
 * Class payHandle
 * @package app\factory
 */
class PayHandle
{

    /**
     * 支付处理判断
     * @param $pay_sn
     * @param $price
     * @param string $pay_mode 支付方式 weixin微信支付  balance余额支付 bean趣豆兑换
     * @return mixed
     */
    public static function pay_handle($pay_sn, $price, $pay_mode = 'weixin')
    {
        //
        //锁-高并发-柱塞
        $file = fopen(__DIR__ . '/lock/pay.txt', 'w+');
        //加锁
        if (flock($file, LOCK_EX)) {
            //TODO 执行业务代码
            $res = self::pay_start($pay_sn, $price, $pay_mode);
            flock($file, LOCK_UN);//解锁
        }
        //关闭文件
        fclose($file);
        return $res;
    }


    //处理支付
    private static function pay_start($pay_sn, $price, $pay_mode = 'weixin')
    {
        Db::startTrans();
        try {
            $pay_log = PayLogModel::getLog($pay_sn);//支付数据
            if (empty($pay_log)) {
                $rsps['code'] = false;
                $rsps['msg'] = "数据不存在";
                return $rsps;
            }
            if ($pay_log['status'] == 1) {//是否已处理
                $rsps['code'] = false;
                $rsps['msg'] = "请勿重复支付";
                return $rsps;
            }
            PayLogModel::updateLog($pay_log, $pay_mode);
            $total = PayLogModel::getLogTotal($pay_sn);
            if ($pay_mode == "weixin") {
                //微信金额单位为分
                $pay_price = intval(round($total * 100));//输出int
            } else {
                $pay_price = floatval($total);
            }
            if ($price != $pay_price) {
                $rsps['code'] = false;
                $rsps['msg'] = "金额不等";
                return $rsps;
            }
            $pay_type = $pay_log['pay_type'];
            //支付类型 pay支付 refund退款 recharge充值 member会员缴费
            switch ($pay_type) {
                case "pay":
                    $order_model = new OrderModel();
                    $order_model->endOrderPay($pay_log['pay_sn'], $pay_mode);
                    break;
                case "recharge":
                    $user_recharge_model = new UserRechargeOrderModel();
                    $user_recharge_model->endOrderPay($pay_log['pay_sn']);
                    break;
                case "member":
                    $user_member_model = new UserMemberOrderModel();
                    $user_member_model->endOrderPay($pay_log['pay_sn']);
                    break;
                case "agency":
                    $agency_order_model = new AgencyOrderModel();
                    $agency_order_model->endOrderPay($pay_log['pay_sn']);
                    break;
                case "deposit":
                    $store_deposit_model = new StoreDepositModel();
                    $store_deposit_model->endDepositPay($pay_log['pay_sn']);
                    break;
                case "refund":
//                    self::refund_order($pay_log);
//                    $user_recharge_model = new UserRechargeOrderModel();
//                    $user_recharge_model->endOrderPay($pay_log['pay_sn']);
                    break;
            }
            //提交事务
            Db::commit();
            $rsps['code'] = true;
            $rsps['msg'] = "成功";
            return $rsps;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $rsps['code'] = false;
            $rsps['msg'] = $e->getMessage();
            return $rsps;
        }

    }

}